package com.efounder.frame.baseui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.utilcode.util.ReflectUtils;


/**
 * mvpfragment 基类
 *
 * @author zzj
 * @version 1.0.0
 */
public abstract class BasePresenterFragment<Presenter extends BaseContract.BasePresenter> extends BaseFragment
        implements BaseContract.BaseView<Presenter> {
    protected static String TAG = "";
    protected Presenter mPresenter;
    protected Activity mActivity;
    protected View mRoot;
    // 标示是否第一次初始化数据
    protected boolean mIsFirstInitData = true;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
        TAG = this.getClass().getSimpleName();
        initArgs(getArguments());
        // 在界面onAttach之后就触发初始化Presenter
        mPresenter = initPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRoot == null) {
            int layId = getContentLayoutId();
            // 初始化当前的跟布局，但是不在创建时就添加到container里边
            View root = inflater.inflate(layId, container, false);
            initView(root);
//            BusManager.getBus().register(this);
            mRoot = root;
        } else {
            if (mRoot.getParent() != null) {
                // 把当前Root从其父控件中移除
                ((ViewGroup) mRoot.getParent()).removeView(mRoot);
            }
        }
        return mRoot;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mIsFirstInitData) {
            // 触发一次以后就不会触发
            mIsFirstInitData = false;
            // 触发
            onFirstInit();
        }
        // 当View创建完成后初始化数据
        initData();
    }

    /**
     * 初始化相关参数
     */
    protected void initArgs(Bundle bundle) {

    }

    /**
     * 初始化Presenter
     *
     * @return Presenter
     */
    protected abstract Presenter initPresenter();

    @Override
    public void setPresenter(Presenter presenter) {
        // View中赋值Presenter
        mPresenter = presenter;
    }


    @Override
    public void showError(int errCode, String str) {

    }

    @Override
    public void showLoading(String text) {
        try {
            ReflectUtils reflectUtils = ReflectUtils.reflect("com.efounder.util.LoadingDataUtilBlack");
            reflectUtils.method("show", _mActivity, text);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void dismissLoading() {
        try {
            ReflectUtils reflectUtils = ReflectUtils.reflect("com.efounder.util.LoadingDataUtilBlack");
            reflectUtils.method("dismiss");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到当前界面的资源文件Id
     *
     * @return 资源文件Id
     */
    @LayoutRes
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     */
    protected abstract void initView(View root);

    /**
     * 初始化监听回调等
     */
    protected void initListener() {

    }

    /**
     * 初始化adpater
     */
    protected void initAdapter() {

    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 当首次初始化数据的时候会调用的方法
     */
    protected void onFirstInit() {

    }

    /**
     * 返回按键触发时调用
     *
     * @return 返回True代表我已处理返回逻辑，Activity不用自己finish。
     * 返回False代表我没有处理逻辑，Activity自己走自己的逻辑
     */
    public boolean onBackPressed() {
        return false;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null && mPresenter.isViewAttached())
            mPresenter.detachView();
    }

}
